package com.future.controller;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.common.exception.AppException;
import com.future.common.global.PlatFormContext;
import com.future.dao.SysUserDao;
import com.future.model.SysUser;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hutool.crypto.SecureUtil;

/**
 * 用户信息controller
 * 
 * @author ThinkPad
 *
 */
@ControllerMapping("/user")
public class SysUserController extends BaseController {
	@Inject
	private SysUserDao sysUserDao;

	public void getUserInfo() {
		SysUser sysUser = this.sysUserDao.findByIdLoadColumns(getCurrentUserId(),
				"userid,username,user_type,userinfoid");
		sysUser.put("name", sysUser.getName());
		renderResult("userinfo", sysUser);
	}

	@Before(Tx.class)
	public void changeCurrentPassword(String passwordold, String passwordnew) {
		SysUser user = PlatFormContext.getCurrentUser();
		if (!SecureUtil.md5(passwordold).equals(user.getPassword())) {
			throw new AppException("旧的密码错误!");
		}
		user.setPassword(SecureUtil.md5(passwordnew));
		user.update();
		renderSuccess();

	}
}
